package com.pub.compress;

import java.util.ArrayList;
/**the Exception to indicate that the dictionary is too large
*/
 class OutDictionaryException extends Exception
      {
        public  String toString()
            {
                return (super.toString ()+"out of the dictionary size!!");
            }
      }
/**
 a dictonry that  contains at most 2^12 words,and should be inited
 at the beginning; it can be looked up,can be added and return the size
@author :Lai Yongxuan  2002.3.10
@version :1.0
*/
public class Dictionary
{
    /** the container of the dictionary,use ArrayList
    *@see java.util.ArrayList
    */
    ArrayList ar=new ArrayList();
    
    /**the constuctor of the class,and put the 128 ASCII to the dictionary  
    */
    public Dictionary()
    {
      // byte i[]=new byte[1];
       char c[]=new char[1];
       for( c[0]=0;c[0]<128;c[0]++)
       {
         
         ar.add(new String(c));
         
       }
    }
    /**return the index number of the word in the dictionary
    */
    public int indexOf(String a)
    {
        return ar.indexOf(a);
    }
    /**add a string to the dictionary
    @param String :the word to be added
    @return NO returned value
    @Exception OutDictionaryException is thrown if the dictionary is too
        large ,it only can contain 4096(2^12) words at most
    */
    public void add (String a) throws OutDictionaryException
    {
       
       if( length()<4096)
           ar.add(a);
       else
       {
         
          throw(new OutDictionaryException());
        
       }
   }
    /** the size  of the dictionary 
    */
    public int length()
    {
    
       return (short)ar.size();
    }
    public String toString()
    {
        Integer le=new Integer(length() );
    
        String str="size of the dictionary: "+le.toString ()+"\n";
        for(int i=0;i<length();i++)
          str+=new String(i+": "+(String)ar.get(i)+"\t");
       return str;
    } 
    /** return the word by the index pointor 
    */
    public String getString(short i)
    {
        return (String)ar.get(i);
    }
    /** only to test the dictionary
    */
    public static void main(String []args )
    {
        Dictionary a=new Dictionary();
    /* try{
        
        for(int i=128;i<6000;i++)
        {
            a.add(new String("i am a student") );
        }
      
      }
      catch(Exception e)
      {  
       
        System.err.println (e.toString());
        
        }*/
       System.out.println(a);
    }
}